package by.pavlyukevich.paymentsystem.panel.card;

import javax.inject.Inject;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.hibernate.LazyInitializationException;

import by.pavlyukevich.paymentsystem.add.ErrorEvent;
import by.pavlyukevich.paymentsystem.add.PanelUpdateEvent;
import by.pavlyukevich.paymentsystem.dataaccess.filter.CardFilter;
import by.pavlyukevich.paymentsystem.datamodel.Account;
import by.pavlyukevich.paymentsystem.datamodel.card.Card;
import by.pavlyukevich.paymentsystem.datamodel.user.Contractor;
import by.pavlyukevich.paymentsystem.datamodel.user.Individual;
import by.pavlyukevich.paymentsystem.datamodel.user.UserAccount;
import by.pavlyukevich.paymentsystem.services.AccountService;
import by.pavlyukevich.paymentsystem.services.card.CardService;
import by.pavlyukevich.paymentsystem.services.user.UserAccountService;

public class CardEditPanel extends Panel {
	@Inject
	CardService cardService;
	@Inject
	AccountService accountService;
	@Inject
	UserAccountService userAccountService;

	public CardEditPanel(String id, Card cardIn, Panel respPanel) {

		super(id);
		if (cardIn.getId() != null) {
			try {
				cardIn.getUserAccount().getIndividual();
			} catch (LazyInitializationException e) {
				cardIn = cardService.getList(new CardFilter().setCard(cardIn)).get(0);
			}
		}
		Card card = cardIn;

		setOutputMarkupId(true);
		Form<Card> form = new Form<Card>("form", new CompoundPropertyModel<Card>(card));
		add(form);

		form.add(new TextField<String>("cardNumber").add(new PropertyValidator<String>()));

		form.add(new DropDownChoice<Account>("account",
				new PropertyModel<Account>(card, "account"), accountService.getList(),
				new IChoiceRenderer<Account>() {
					@Override
					public Object getDisplayValue(Account account) {
						return account.getAccountNumber();
					}

					@Override
					public String getIdValue(Account object, int index) {
						return Integer.toString(index);
					}
				}).add(new PropertyValidator<Account>()));

		form.add(new DropDownChoice<UserAccount>("owner", new PropertyModel<UserAccount>(card,
				"userAccount"), userAccountService.getList(), new IChoiceRenderer<UserAccount>() {
			@Override
			public Object getDisplayValue(UserAccount userAccount) {
				Individual individual = userAccount.getIndividual();
				Contractor contractor = userAccount.getContractor();
				return (individual != null) ? individual.getFamily() + " " + individual.getName()
						: contractor.getName();
			}

			@Override
			public String getIdValue(UserAccount object, int index) {
				return Integer.toString(index);
			}
		}).add(new PropertyValidator<Account>()));

		form.add(new CheckBox("isBlocked").add(new PropertyValidator<Boolean>()));

		form.add(new AjaxSubmitLink("save") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				cardService.saveOrUpdate(card);
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target, respPanel));
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				send(getPage(), Broadcast.BREADTH, new ErrorEvent(target));
			}

		});
	}

}
